<template>
	<BaseContainer>
		<ProjectSelect v-if="!project?.id" belongProject="elderlyMeal"></ProjectSelect>
		<aplid-base-crud
			ref="crud"
			class="w-100"
			v-bind="baseConfig"
			:saveFormate="saveFormate"
			:paramsObj="{
				subprojectId: project?.id,
				belongProject: project?.belongProject,
				subprojectName: project?.projectName,
			}"
		>
			<template v-slot:ruleConfigForm="{ row, model }">
				<el-scrollbar max-height="30rem">
					<AplidForm ref="ruleSetEl" v-model="model.ruleConfig" v-bind="ruleSetConfig" :showBtn="false" :showRules="true"> </AplidForm>
				</el-scrollbar>
			</template>
			<template v-slot:ruleStatus="{ row }">
				<el-switch
					:model-value="row.ruleStatus"
					class="ml-2"
					inline-prompt
					@click.native="handleOnChange(row)"
					:active-value="1"
					:inactive-value="0"
					style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
					active-text="启用"
					inactive-text="停用"
				/>
			</template>
		</aplid-base-crud>
	</BaseContainer>
</template>

<script setup lang="ts">
import { baseConfig, ruleSetConfig } from './config'
import ProjectSelect from '@/views/homeCare/components/ProjectSelect/index.vue'
import apiMod from '@/apiMod/index'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { useCommonStore } from '@/stores/common'

const props = defineProps<{
	project?: any
}>()

const crud = ref(null)
const reloadKey = ref(0)
const ruleSetEl = ref(null)

const saveFormate = (form) => {
	const { queryObj } = useCommonStore()

	return new Promise(async(resolve, reject) => {
		try {
			await ruleSetEl.value.onSubmit()
			const rule = form.ruleConfig || {}
			const ruleConfig = {
				serviceRule: {
					age: rule.age,
					distance: rule.distance,
					multiOrgService: rule.multiOrgService,
					allowMultiSupItem: rule.allowMultiSupItem,
					maxSupItems: rule.maxSupItems,
					allowMultiSubItem: rule.allowMultiSubItem,
					maxSubItems: rule.maxSubItems,
					calcEndServiceLength: rule.calcEndServiceLength,
					limitElderlyDayOrder: rule.limitElderlyDayOrder,
					limitElderlyDayOrderNum: rule.limitElderlyDayOrderNum,
					stepElderlyServiceType: rule.stepElderlyServiceType,
					stepElderlyServiceLimit: rule.stepElderlyServiceLimit,
					stepStaffServiceType: rule.stepStaffServiceType,
					stepStaffServiceLimit: rule.stepStaffServiceLimit,
				},
				orderRule: {
					allowService: rule.allowService,
					allowServiceBetween: rule.allowServiceBetween?.toString(),
					maxServiceLength: rule.maxServiceLength,
					allowCareService: rule.allowCareService,
					allowHelpFood: rule.allowHelpFood,
					breakfastBetween: rule.breakfastBetween?.toString(),
					lunchBetween: rule.lunchBetween?.toString(),
					dinnerBetween: rule.dinnerBetween?.toString(),
					helpFoodValidFirst: rule.helpFoodValidFirst,
          allowSpecialDisabilityService: rule.allowSpecialDisabilityService,
					allowSpecialOrgService: rule.allowSpecialOrgService,
					allowSpecialBedService: rule.allowSpecialBedService,
					allowSpecialAidService: rule.allowSpecialAidService,
					allowSpecialCadreService: rule.allowSpecialCadreService,
				},
				processRule: {
					startStaffFace: rule.startStaffFace,
					startElderlyFace: rule.startElderlyFace,
					endStaffFace: rule.endStaffFace,
					endElderlyFace: rule.endElderlyFace,
					beforeServicePicRequired: rule.beforeServicePicRequired,
					beforeServicePicNum: rule.beforeServicePicNum,
					middleServicePicRequired: rule.middleServicePicRequired,
					middleServicePicNum: rule.middleServicePicNum,
					allowRadio: rule.allowRadio,
					afterServicePicRequired: rule.afterServicePicRequired,
					allowRadioStartMin: rule.allowRadioStartMin,
					afterServicePicNum: rule.afterServicePicNum,
				},
			}
			resolve({
				...form,
				ruleConfig,
				projectId: queryObj.subprojectId,
				projectName: queryObj.subprojectName,
			})
		} catch (error) {
			reject(error)
		}
	})
}
const handleOnChange = (row) => {
	const userStore = useUserStore()
	// 判断有没有权限
	const all_auth = userStore.permissions
	if (!all_auth[baseConfig.auth.edit]) {
		return
	}

	apiMod[baseConfig.api.edit]({
		id: row.id,
		ruleStatus: row.ruleStatus == 0 ? 1 : 0,
	}).then(() => {
		ElMessage.success('操作成功')
		reloadKey.value += 1
		crud.value?.refreshChange()
	})
}
</script>

<style lang="scss" scoped></style>
